#include "Data.h"

/*
 *--------------------------------------------------------------------------------------
 *       Class:  Data
 *      Method:  Data
 * Description:  Constrói uma nova entrada/saída passado a quantidade de cada variável
 *--------------------------------------------------------------------------------------
 */
Data::Data(uint nInVars, uint nOutVars, int filetype, CSVFilePtr csv)
{
    input = vdoublePtr(new vdouble);
    output = vdoublePtr(new vdouble);
    neuralOutput = vdoublePtr(new vdouble);

    // Input
    csv->readV(*input, nInVars);

    // Output
    csv->readV(*output, nOutVars);

    // Neural Output
    if(filetype == OUTPUT)
        csv->readV(*neuralOutput, nOutVars);
    else
        neuralOutput->resize(nOutVars);
}

/*
 *--------------------------------------------------------------------------------------
 *       Class:  Data
 *      Method:  hasSucceeded
 * Description:  Verifica se o cálculo da rede neural foi bem sucedido para esta
 *               entrada/saída
 *--------------------------------------------------------------------------------------
 */
bool Data::hasSucceeded(double tolerance)
{
    for(uint i = 0; i < output->size(); i++)
    {
        double diff = output->at(i) - neuralOutput->at(i);
        if(fabs(diff) >= tolerance)
            return false;
    }
    return true;
}

/*
 *--------------------------------------------------------------------------------------
 *       Class:  Data
 *      Method:  setNeuralOutput
 * Description:  Seta valores de saída da rede neural
 *--------------------------------------------------------------------------------------
 */
void Data::setNeuralOutput(vdoublePtr out)
{
    for(uint i = 0; i < neuralOutput->size(); i++)
        neuralOutput->at(i) = out->at(i);
}

/*
 *--------------------------------------------------------------------------------------
 *       Class:  Data
 *      Method:  printInput
 * Description:  Imprime as entradas
 *--------------------------------------------------------------------------------------
 */
void Data::printInput()
{
    for(uint i = 0; i < input->size(); i++)
        cout << input->at(i) << " ";
}

/*
 *--------------------------------------------------------------------------------------
 *       Class:  Data
 *      Method:  printOutput
 * Description:  Imprime as saídas
 *--------------------------------------------------------------------------------------
 */
void Data::printOutput()
{
    for(uint i = 0; i < output->size(); i++)
        cout << output->at(i) << " ";
}

/*
 *--------------------------------------------------------------------------------------
 *       Class:  Data
 *      Method:  printNeuralOutput
 * Description:  Imprime as saídas da rede neural
 *--------------------------------------------------------------------------------------
 */
void Data::printNeuralOutput()
{
    for(uint i = 0; i < neuralOutput->size(); i++)
    {
        double out = neuralOutput->at(i);
        double bout = (out >= 0.5) ? 1 : 0;
        cout << out << " (" << bout << ") ";
    }
}

